package net.mcreator.makecustomitem.procedure;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.World;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.DamageSource;
import net.minecraft.potion.PotionEffect;
import net.minecraft.item.ItemStack;
import net.minecraft.init.MobEffects;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;

import net.mcreator.makecustomitem.ElementsMakecustomitemMod;

import java.util.Map;

@ElementsMakecustomitemMod.ModElement.Tag
public class ProcedureAnShaUse extends ElementsMakecustomitemMod.ModElement {
	public ProcedureAnShaUse(ElementsMakecustomitemMod instance) {
		super(instance, 24);
	}

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			System.err.println("Failed to load dependency entity for procedure AnShaUse!");
			return;
		}
		if (dependencies.get("sourceentity") == null) {
			System.err.println("Failed to load dependency sourceentity for procedure AnShaUse!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		Entity sourceentity = (Entity) dependencies.get("sourceentity");
		if (((((sourceentity instanceof EntityLivingBase) ? ((EntityLivingBase) sourceentity).getHeldItemMainhand() : ItemStack.EMPTY)
				.hasTagCompound()
						? ((sourceentity instanceof EntityLivingBase) ? ((EntityLivingBase) sourceentity).getHeldItemMainhand() : ItemStack.EMPTY)
								.getTagCompound().getDouble("Assassination")
						: -1) > 0)) {
			if (((entity.getHorizontalFacing()) == (sourceentity.getHorizontalFacing()))) {
				if (((((sourceentity instanceof EntityLivingBase) ? ((EntityLivingBase) sourceentity).getHeldItemMainhand() : ItemStack.EMPTY)
						.hasTagCompound()
								? ((sourceentity instanceof EntityLivingBase)
										? ((EntityLivingBase) sourceentity).getHeldItemMainhand()
										: ItemStack.EMPTY).getTagCompound().getDouble("Assassination")
								: -1) <= (((sourceentity instanceof EntityLivingBase) ? ((EntityLivingBase) sourceentity).getMaxHealth() : -1)
										/ 20))) {
					entity.attackEntityFrom(DamageSource.GENERIC,
							(float) (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1)
									- ((((sourceentity instanceof EntityLivingBase)
											? ((EntityLivingBase) sourceentity).getHeldItemMainhand()
											: ItemStack.EMPTY).hasTagCompound()
													? ((sourceentity instanceof EntityLivingBase)
															? ((EntityLivingBase) sourceentity).getHeldItemMainhand()
															: ItemStack.EMPTY).getTagCompound().getDouble("Assassination")
													: -1)
											* 20)));
				} else {
					entity.attackEntityFrom(DamageSource.GENERIC,
							(float) (((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHealth() : -1)
									- ((((sourceentity instanceof EntityLivingBase)
											? ((EntityLivingBase) sourceentity).getHeldItemMainhand()
											: ItemStack.EMPTY).hasTagCompound()
													? ((sourceentity instanceof EntityLivingBase)
															? ((EntityLivingBase) sourceentity).getHeldItemMainhand()
															: ItemStack.EMPTY).getTagCompound().getDouble("Assassination")
													: -1)
											* 20)));
					if (sourceentity instanceof EntityPlayer && !sourceentity.world.isRemote) {
						((EntityPlayer) sourceentity).sendStatusMessage(
								new TextComponentString(
										"\u00A7c\u5F53\u524D\u6B66\u5668\u8FC7\u4E8E\u5F3A\u608D\uFF0C\u8EAB\u4F53\u53D7\u5230\u4E86\u4F24\u5BB3"),
								(false));
					}
					if (sourceentity instanceof EntityLivingBase)
						((EntityLivingBase) sourceentity)
								.addPotionEffect(new PotionEffect(MobEffects.MINING_FATIGUE, (int) 40, (int) 0, (false), (true)));
					if (sourceentity instanceof EntityLivingBase)
						((EntityLivingBase) sourceentity).addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, (int) 40, (int) 0, (false), (true)));
					if (sourceentity instanceof EntityLivingBase)
						((EntityLivingBase) sourceentity)
								.addPotionEffect(new PotionEffect(MobEffects.INSTANT_DAMAGE, (int) 10, (int) 0, (false), (true)));
				}
			}
		}
	}

	@SubscribeEvent
	public void onEntityAttacked(LivingAttackEvent event) {
		if (event != null && event.getEntity() != null) {
			Entity entity = event.getEntity();
			int i = (int) entity.posX;
			int j = (int) entity.posY;
			int k = (int) entity.posZ;
			World world = entity.world;
			java.util.HashMap<String, Object> dependencies = new java.util.HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			dependencies.put("sourceentity", event.getSource().getImmediateSource());
			this.executeProcedure(dependencies);
		}
	}

	@Override
	public void preInit(FMLPreInitializationEvent event) {
		MinecraftForge.EVENT_BUS.register(this);
	}
}
